package main

import (
	"fmt"
	"math/rand"
	"time"
)

// 生成器随机睡眠1-2000ms并返回一个数值,供给select测试使用
func generator() chan int {
	seed := make(chan int)
	go func() {
		i := 0
		for {
			// 随机睡眠 1~2000ms
			time.Sleep(time.Duration(rand.Intn(2001)) * time.Millisecond)
			seed <- i
			i++
		}
	}()
	return seed
}

// 创建一个消费者
func createConsumer() chan int {
	c := make(chan int)
	go func() {
		for n := range c {
			fmt.Println("当前消费者收到数据:", n)
		}
	}()
	return c
}

func main() {

	c1, c2 := generator(), generator()
	consumer := createConsumer()

	for {
		select {
		case n := <-c1:
			consumer <- n
		case n := <-c2:
			consumer <- n
		}
	}
}
